//
//  BLShopingPointViewController.m
//  blanket
//
//  Created by Mac on 2017/12/7.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLSelfCustomViewController.h"
#import "BLShopIngPointTableViewCell.h"
#import "BLShopingDetailViewController.h"
#import "BLShopModel.h"
#import "STNoresultView.h"
@interface BLSelfCustomViewController ()
@property(nonatomic, assign) NSInteger                     page;
@property(nonatomic, strong) NSMutableArray <BLShopModel*>                    *dataSouce;
@property(nonatomic, strong) STNoresultView                     *noresultView;
@end

@implementation BLSelfCustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"私人定制";
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self sendSelfCustomListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self sendSelfCustomListRequest];
    }];
    self.tableView.mj_footer.hidden = YES;
    self.noresultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 180, UIScreenWidth, 300)
                                                        title:@"抱歉,暂无商品"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                     
                                                 }];
    self.noresultView.hidden = YES;
    [self.tableView addSubview:self.noresultView];
    // Do any additional setup after loading the view.
}
- (void)reloadData{
    self.noresultView.hidden = self.dataSouce.count;
    if (self.dataSouce.count >= 10) {
        self.tableView.mj_footer.hidden = NO;
    }else{
        self.tableView.mj_footer.hidden = YES;
    }
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BLShopIngPointTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLShopIngPointTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLShopIngPointTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
//    cell.priceLable.hidden = YES;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BLShopingDetailViewController * vc = [BLShopingDetailViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)sendSelfCustomListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/getGatherScoreList"];
    
    [dic setObject:@"3" forKey:@"type"];
    [dic setObject:@(self.page) forKey:@"page"];
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [self.tableView.mj_footer endRefreshing];
                                                 if (self.page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                 }
                                                 NSArray * dataArray = responseObject[@"data"];
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     BLShopModel * mdoel = [BLShopModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:mdoel];
                                                 }
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [self.tableView.mj_footer endRefreshing];
                                                  [self reloadData];
                                                 [SVProgressHUD dismiss];
                                                // [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end


